<template>
  <div id="app">
	<!-- Header 导航栏 -->
	<header id="header" class="alt">
	 <div class="logo">InkCloud <span style="font-family: '微软雅黑';">墨云自习</span></div>
	 <a>
		 <span v-if="!this.$store.state.isLogin" @click="ChangeState"> ≡ Log in </span>
		 <span v-else @click="ChangeState">already logged in</span>
	</a>
	</header>

	 <!-- menu 用户菜单栏 -->
	 <div v-show="isShow" id="menu" class="popIn">
		 <img  class="menu_img"  :src="bgd_img" />
		 <div class="menu_info">
			 <div>用户名：{{this.$store.state.UserInfo.username}}</div>
			 <div>性别：{{this.$store.state.UserInfo.gender}}</div>
		 </div>
		 <hr />
		 <div class="menu_func">
			 
			 <a @click="ChangeInfo(1)" style="cursor: pointer;">修改密码</a><br />
			 <a @click="logout" style="cursor: pointer;">退出登录</a><br />
			 <router-link to="/personal" style="color: rgb(5,117,202);text-decoration: underline;">管理个人资料设置</router-link>
		 </div>
	 </div>
	 
	 <!-- 修改密码窗口 -->
	 <div v-show="isChange" id="change" class="changepopIn">
		旧密码：<input v-model="oldpassword" name="oldpassword" type="password" placeholder="旧密码" />
	    新密码：<input v-model="newpassword" name="newpassword" type="password" placeholder="新密码" />
	    <button style="margin-right: 10%;" @click="ChangePassword(1)">确定</button>
		<button @click="ChangePassword(0)">取消</button>
	 </div>
	 
	 
   <!-- Banner 中央标题 -->
   	<section id="banner">
   		<h1>Welcome to InkCloud</h1>
		<p>A free responsive study space for you.</p>
   	</section>
	
	<!-- IndexArea 功能区 -->
	<index-area :enterlist="enterlist"></index-area>
	 
  </div> 
</template>


<script>

import IndexArea from '../components/IndexArea.vue'


export default {
  name: 'app',
  
  components: {
    
	IndexArea
  },

  data:function(){
	  return {
		  isShow:false,      //个人简介窗口显示标志
		  isChange:false,    //修改密码窗口显示标志
		  oldpassword:"",    //旧密码
		  newpassword:"",    //新密码
		  bgd_img:require("../assets/img/login.png"), //背景图片
		  
		  //功能列表
		  enterlist:[
			  {
			  	 headerimg:require("../assets/img/ico/room_1.png"),  //图像
			  	 title:"默声自习室",
			  	 detail:"Study Room",
				 link: this.$myConfig.studyRoomAddr  //跳转链接
			  },
			  {
			  	 headerimg:require("../assets/img/ico/talk_1.png"),
			  	 title:"社区",
			  	 detail:"Community",
				 link:this.$myConfig.communityAddr
			  },
			  {
			  	 headerimg:require("../assets/img/ico/search_1.png"),
			  	 title:"资源共享",
			  	 detail:"To search Resource",
				 link:this.$myConfig.resourceAddr
			  },
			  {
			  	 headerimg:require("../assets/img/ico/shopping.png"),
			  	 title:"墨云商城",
			  	 detail:"Shop",
			  	link: this.$myConfig.shopAddr 
			  },
			  {
			  	 headerimg:require("../assets/img/ico/map.png"),
			  	 title:"用户分布",
			  	 detail:"Distribution",
			  	link: this.$myConfig.distributionAddr 
			  }
		  ],
		  		 
	  }
  },
  
  //页面挂载后获取cookie
  //coolie存在则直接登录
  mounted:function(){
	  
	  var server = this.$myConfig.server
	  
  	  var userid = this.cookie.getCookie("userid");
  	  
  	  if (userid != null && userid != '') {
		  console.log("通过考验")
  	    this.$store.state.UserInfo.userid = userid;
		
		//更新定位
		const geolocation = new BMap.Geolocation();
		var _this = this;
		geolocation.getCurrentPosition(function getinfo(position){
		    let province = position.address.province;     //获取省份信息
		    _this.$store.state.UserInfo.province = province;
			console.log(province);
			let data={
				province:_this.$store.state.UserInfo.province,
			}
			this.$axios({
			method: 'post',
			url: server + "/user/changeProvince",
			withCredentials:true,
			data:data,
			}).then((res)=>{
						  if(res.data.status == 1){
						      console.log("省份更新成功");
						      }else{
						      console.log("省份更新失败！");	  
						            }
				    })
		  }, function(e) {
			  this.$message.error('定位失败！')
		                 }, {provider: 'baidu'});   
						 
						 
  		//赋予信息
  		this.$axios({
			method: 'get',
			url: server + "/user/userinfo",
			withCredentials:true,
			}).then((res)=>{
  		this.$store.state.UserInfo.age = res.data.data.age;
  		this.$store.state.UserInfo.email = res.data.data.email;
  		this.$store.state.UserInfo.gender = res.data.data.gender;
  		this.$store.state.UserInfo.introduction = res.data.data.introduction;
  		this.$store.state.UserInfo.username = res.data.data.username; 	  		                     
  		})
  		this.$store.state.isLogin = true;
		
  	  }  
  	   
  },
  
  methods: {
	  // 登录 or 显示简介
	  // 已登录，则显示简介，未登录，则跳转登录界面
  	  ChangeState:function(event){
		  if(this.$store.state.isLogin){
			  this.isShow = !this.isShow
		  }
  		  else {
			  this.$router.push({
				  path:"/login"
			  })
		  }
  	  },
	  
	  //修改密码界面显示
	  ChangeInfo:function(data){
		  if(data) this.isChange = true;
	      else     this.isChange = false
	  }, 
	  
	  //退出
	  //修改登录状态，初始化全局用户信息变量userInfo
	  //个人简介、修改密码窗口均初始化为不显示
	  logout:function(){
		  this.$store.state.isLogin = false;
		  this.$store.commit("initUserInfo"); 
		  this.isShow = false;
		  this.isChange = false;
		 
		  this.cookie.clearCookie('userid')
		  this.cookie.clearCookie('token')
		  this.cookie.clearCookie('province')
	  },
	  
	  //修改密码
	  //将修改的密码更新到store的userInfo
	  //同时将newpassword和oldpassword初始化，关闭修改密码窗口
	  ChangePassword:function(edata){
		  if(edata){
				  let data = {
					  userid:this.$store.state.UserInfo.userid,
					  oldPassword:this.oldpassword,
					  newPassword:this.newpassword,
				              }
	var server = this.$myConfig.server
	this.$axios({
		method: 'post',
		url: server + "/user/changePwd",
		withCredentials:true,
		data:data
		}).then((res)=>{
			if(res.data.status == -1){
				this.timer = setTimeout(()=>{  //设置延迟执行
				  this.$router.push("/login");
				},1000);
			}
			 else if(res.data.status == 1){
					this.$message({
					          message: '修改密码成功',
					          type: 'success'
					        });
					this.newpassword = "";
					this.oldpassword = "";
			   }else{
				   this.$message.error('修改密码失败！');
				   this.newpassword = "";
				   this.oldpassword = "";
					 }  
		})
	}
	    //不符合修改条件直接初始化新密码和旧密码
		 else{
			 this.newpassword = "";
			 this.oldpassword = "";
		      }
		  this.isChange = false;
},
	  
   
}	  
}

</script> 


<style>
html,body,#app {
  margin: 0;
  padding: 0;
  font-family: "Raleway", Arial, Helvetica, sans-serif;
}
</style>

<style scoped> 

#menu{
    margin-top: 80px;
	margin-left: 80%;
	float:left;
	position: absolute;
	background-color: #fff;
	width: 18%;
	border-radius: 10px;
	padding: 20px;
	box-sizing: border-box;
	font-family: "黑体";
	z-index: 1;
}

#change{
	opacity: 0.95;
	background-color: #fff;
	border: 1px solid rgba(0,0,0,.05);
	border-radius: 10px;
	box-shadow: rgb(0 0 0 / 10%) 0 20px 50px;
	padding: 30px;
	position: absolute;
	top:50%;
	left:50%;
	transform: translate(-50%,-50%);
	transition: all .25s;
	width: 350px;
	text-align: center;
}

input{
	background-color: #fff;
	border: 1px solid rgba(0,0,0,.1);
	border-radius: 5px;
	display: inline-block;
	font-size: 14px;
	margin: 15px 0;
	padding: 10px 15px;
	width: 70%;
	transition: border-color .2s;
		
}

button{
	background-color: rgb(102,102,102);
	border-radius: 5px;
	border-style: none;
	color: #fff;
	cursor: pointer;
	display:inline-block;
	font-size: 16px;
	margin: 15px 0;
	padding: 10px 15px;
	transition: all .25s;
	width: 40%;
}

button:hover {
    filter: brightness(1.1);
}

/*弹层动画（放大）*/
.popIn {
    -webkit-animation: fadeleftIn .4s;
    animation: fadeleftIn .4s;
    -webkit-animation-name: popIn;
    animation-name: popIn;
}

@-webkit-keyframes popIn {
    0% {
        -webkit-transform: scale3d(0, 0, 0);
        transform: scale3d(0.5, 0.5, 0.5);
        opacity: 0;
    }
    50% {
        -webkit-animation-timing-function: cubic-bezier(0.47, 0, 0.745, 0.715);
        animation-timing-function: cubic-bezier(0.47, 0, 0.745, 0.715);
    }
    100% {
        -webkit-transform: scale3d(1, 1, 1);
        transform: scale3d(1, 1, 1);
        -webkit-animation-timing-function: cubic-bezier(0.25, 0.46, 0.45, 0.94);
        animation-timing-function: cubic-bezier(0.25, 0.46, 0.45, 0.94);
        opacity: 1;
    }
}

@keyframes popIn {
    0% {
        -webkit-transform: scale3d(0, 0, 0);
        transform: scale3d(0.5, 0.5, 0.5);
        opacity: 0;
    }
    50% {
        -webkit-animation-timing-function: cubic-bezier(0.47, 0, 0.745, 0.715);
        animation-timing-function: cubic-bezier(0.47, 0, 0.745, 0.715);
    }
    100% {
        -webkit-transform: scale3d(1, 1, 1);
        transform: scale3d(1, 1, 1);
        -webkit-animation-timing-function: cubic-bezier(0.25, 0.46, 0.45, 0.94);
        animation-timing-function: cubic-bezier(0.25, 0.46, 0.45, 0.94);
        opacity: 1;
    }
}

.changepopIn {
    -webkit-animation: fadeleftIn .4s;
    animation: fadeleftIn .4s;
    -webkit-animation-name: changepopIn;
    animation-name: changepopIn;
}
@-webkit-keyframes changepopIn {
    0% {
        -webkit-transform: scale3d(0, 0, 0);
        transform: scale3d(0.5, 0.5, 0.5);
        opacity: 0;
    }
}

@keyframes changepopIn {
    0% {
        -webkit-transform: scale3d(0, 0, 0);
        transform: scale3d(0.5, 0.5, 0.5);
        opacity: 0;
    }
}
.menu_img{
	width: 50px;
	display: inline-block;
	vertical-align:middle
}

.menu_info{
	display: inline-block;
	vertical-align:middle;
	margin-left: 20px;
	font-size: 13px;
	font-weight: 500;
	line-height: 150%;
}

.menu_func{
	margin-top: 15px;
	font-size: 15px;
	font-weight: 500;
	line-height: 150%;
}

.menu_func a{
	text-decoration: none;
	color: black;
}

/* Header 导航栏*/
#header {
	font-size: 1.25em;
	background: rgba(0, 0, 0, 0.75);
	color: #aed6f1;
	height: 3.25em;
	left: 0;
	line-height: 3.25em;
	position: fixed;
	text-align: right;
	top: 0;
	width: 100%;
	cursor:default;
	z-index: 10001;
}

#header > .logo {
	display: inline-block;
	height: inherit;
	left: 1.25em;
	line-height: inherit;
	margin: 0;
	padding: 0;
	position: absolute;
	top: 0;
}

#header > .logo span {
	font-weight: 300;
	font-size: .8em;
	color: #FFF;
}

#header > a {
	transition: color 0.2s ease-in-out;
	display: inline-block;
	padding: 0 1.25em;
	text-decoration: none;
	color: #FFF;
}

#header > a:hover {
	color: #3498db;
}

/* 背景透明,位置调整 */
#header.alt {
	animation: none;
	background-color: transparent;
	box-shadow: none;
	overflow: hidden;
	position: absolute;
	top: 1em;
}

/* Banner 背景*/
#banner {
	padding: 12em 0 10em 0;
	background-size: cover;
	background-position: top;
	background-attachment: fixed;
	background-repeat: no-repeat;
	text-align: center;
	background-image: url(../assets/img/banner.jpg);
}

#banner h1 {
	font-size: 3.5em;
	font-weight: 100;
	color: #fff;
	line-height: 1em;
	margin: 0 0 0.5em 0;
	padding: 0;
}

#banner p {
	font-size: 1em;
	color: rgba(255, 255, 255, 0.55);
	margin-bottom: 1.75em;
}


</style>
